﻿using System;
using UnityEngine;

namespace UnityStandardAssets.ImageEffects
{
	// Token: 0x0200009F RID: 159
	[RequireComponent(typeof(Camera))]
	[ExecuteInEditMode]
	[AddComponentMenu("Image Effects/Mucus")]
	public class MucusImageEffect : PostEffectsBase
	{
		// Token: 0x06000513 RID: 1299 RVA: 0x0001DB6C File Offset: 0x0001BD6C
		private void OnEnable()
		{
			Camera component = base.GetComponent<Camera>();
		}

		// Token: 0x06000514 RID: 1300 RVA: 0x0001DB80 File Offset: 0x0001BD80
		public override bool CheckResources()
		{
			base.CheckSupport(false);
			this.SetupMaterial(this.downShader, ref this.downMaterial);
			this.SetupMaterial(this.gaussShader, ref this.gaussMaterial);
			this.SetupMaterial(this.toInfoShader, ref this.toInfoMaterial);
			this.SetupMaterial(this.finalShader, ref this.finalMaterial);
			if (!this.isSupported)
			{
				base.ReportAutoDisable();
			}
			return this.isSupported;
		}

		// Token: 0x06000515 RID: 1301 RVA: 0x0001DBF4 File Offset: 0x0001BDF4
		private void SetupMaterial(Shader shader, ref Material material)
		{
			material = base.CheckShaderAndCreateMaterial(shader, material);
		}

		// Token: 0x06000516 RID: 1302 RVA: 0x0001DC04 File Offset: 0x0001BE04
		private void OnRenderImage(RenderTexture source, RenderTexture destination)
		{
			if (!this.CheckResources() || this.CameraSetRendTex == null)
			{
				Graphics.Blit(source, destination);
				return;
			}
			if (this.camera == null)
			{
				this.camera = base.GetComponent<Camera>();
			}
			int width = source.width / this.downDivider;
			int height = source.height / this.downDivider;
			RenderTexture temporary = RenderTexture.GetTemporary(width, height, 0, source.format);
			RenderTexture temporary2 = RenderTexture.GetTemporary(width, height, 0, source.format);
			RenderTexture temporary3 = RenderTexture.GetTemporary(source.width, source.height, 0, source.format);
			this.GetSampleOffsets_DownScale4x4(source.width, source.height, ref this.downOffsets);
			for (int i = 0; i < 16; i++)
			{
				this.downMaterial.SetVector("_Offsets" + i, this.downOffsets[i]);
			}
			Graphics.Blit(source, temporary, this.downMaterial);
			this.Gaussian8(temporary, temporary2, this.gauss8Angle, this.gauss8SampleLen, this.gauss8Dispersion);
			this.Gaussian8(temporary2, temporary, this.gauss8Angle + 90f, this.gauss8SampleLen, this.gauss8Dispersion);
			this.Gaussian8(temporary, temporary2, this.gauss8Angle, this.gauss8SampleLen, this.gauss8Dispersion);
			this.Gaussian8(temporary2, temporary, this.gauss8Angle + 90f, this.gauss8SampleLen, this.gauss8Dispersion);
			this.toInfoMaterial.SetFloat("_PixelX", (float)Screen.width);
			this.toInfoMaterial.SetFloat("_PixelY", (float)Screen.height);
			this.toInfoMaterial.SetFloat("_NormalScale", this.toInfoNormalScale);
			this.toInfoMaterial.SetFloat("_Pow", this.toInfoPow);
			this.toInfoMaterial.SetFloat("_Mul", this.toInfoMul);
			Graphics.Blit(temporary, temporary3, this.toInfoMaterial);
			this.finalMaterial.SetTexture("_MetaTex", this.CameraSetRendTex.RendTex);
			this.finalMaterial.SetFloat("_BumpRate", this.finalBumpRate);
			this.finalMaterial.SetFloat("_DistortionRate", this.finalDistortionRate);
			this.finalMaterial.SetFloat("_MuddyRate", this.finalMuddyRate);
			this.finalMaterial.SetFloat("_AlphaPow", this.finalAlphaPow);
			this.finalMaterial.SetFloat("_EnvRate", this.finalEnvRate);
			this.finalMaterial.SetColor("_MuddyMaxColor", this.finalMuddyMaxColor);
			this.finalMaterial.SetColor("_MuddyMinColor", this.finalMuddyMinColor);
			this.finalMaterial.SetTexture("_Env", this.finalEnv);
			this.finalMaterial.SetColor("_EnvColor", this.finalEnvColor);
			Quaternion rotation = base.transform.rotation;
			float num = this.camera.fieldOfView * 0.5f;
			float num2 = this.camera.fieldOfView * 0.5f * this.camera.aspect;
			Vector3 v = rotation * (Quaternion.Euler(0f, -num2, 0f) * Vector3.forward);
			Vector3 v2 = rotation * (Quaternion.Euler(0f, num2, 0f) * Vector3.forward);
			Vector3 v3 = rotation * (Quaternion.Euler(num, 0f, 0f) * Vector3.forward);
			Vector3 v4 = rotation * (Quaternion.Euler(-num, 0f, 0f) * Vector3.forward);
			this.finalMaterial.SetVector("_ViewVecL", v);
			this.finalMaterial.SetVector("_ViewVecR", v2);
			this.finalMaterial.SetVector("_ViewVecU", v3);
			this.finalMaterial.SetVector("_ViewVecD", v4);
			Graphics.Blit(temporary3, destination, this.finalMaterial);
			RenderTexture.ReleaseTemporary(temporary);
			RenderTexture.ReleaseTemporary(temporary2);
			RenderTexture.ReleaseTemporary(temporary3);
		}

		// Token: 0x06000517 RID: 1303 RVA: 0x0001E034 File Offset: 0x0001C234
		private void GetSampleOffsets_DownScale4x4(int width, int height, ref Vector2[] offsets)
		{
			float num = 1f / (float)width;
			float num2 = 1f / (float)height;
			int num3 = 0;
			for (int i = 0; i < 4; i++)
			{
				for (int j = 0; j < 4; j++)
				{
					offsets[num3].x = ((float)j - 1.5f) * num;
					offsets[num3].y = ((float)i - 1.5f) * num2;
					num3++;
				}
			}
		}

		// Token: 0x06000518 RID: 1304 RVA: 0x0001E0B0 File Offset: 0x0001C2B0
		private void Gaussian8(Texture source, RenderTexture dest, float angle, float sampleLen, float dispersion)
		{
			float[] values = new float[8];
			this.CalcWeight_Gauss8(ref values, dispersion);
			Vector4[] array = new Vector4[8];
			float num = Mathf.Sin(angle * 0.017453292f);
			float num2 = Mathf.Cos(angle * 0.017453292f);
			Vector2 a = default(Vector2);
			a.x = num / (float)source.width * sampleLen;
			a.y = num2 / (float)source.height * sampleLen;
			for (int i = 0; i < 8; i++)
			{
				array[i] = a * (float)i;
			}
			for (int j = 0; j < 8; j++)
			{
				this.gaussMaterial.SetVectorArray("_Offsets", array);
				this.gaussMaterial.SetFloatArray("_Weights", values);
			}
			Graphics.Blit(source, dest, this.gaussMaterial, 1);
		}

		// Token: 0x06000519 RID: 1305 RVA: 0x0001E198 File Offset: 0x0001C398
		private void CalcWeight_Gauss8(ref float[] weight, float dispersion)
		{
			float num = 0f;
			for (int i = 0; i < 8; i++)
			{
				float num2 = 1f + 2f * (float)i;
				float num3 = Mathf.Exp(-0.5f * (num2 * num2) / dispersion);
				num += 2f * num3;
				weight[i] = num3;
			}
			for (int i = 0; i < 8; i++)
			{
				weight[i] /= num;
			}
		}

		// Token: 0x040003F1 RID: 1009
		public Shader downShader;

		// Token: 0x040003F2 RID: 1010
		private Material downMaterial;

		// Token: 0x040003F3 RID: 1011
		private Vector2[] downOffsets = new Vector2[16];

		// Token: 0x040003F4 RID: 1012
		[Range(1f, 16f)]
		public int downDivider = 2;

		// Token: 0x040003F5 RID: 1013
		public Shader gaussShader;

		// Token: 0x040003F6 RID: 1014
		private Material gaussMaterial;

		// Token: 0x040003F7 RID: 1015
		private float gauss8Angle;

		// Token: 0x040003F8 RID: 1016
		private float gauss8Dispersion = 100f;

		// Token: 0x040003F9 RID: 1017
		[Range(0f, 2f)]
		public float gauss8SampleLen = 1f;

		// Token: 0x040003FA RID: 1018
		public Shader toInfoShader;

		// Token: 0x040003FB RID: 1019
		private Material toInfoMaterial;

		// Token: 0x040003FC RID: 1020
		public float toInfoNormalScale = 1f;

		// Token: 0x040003FD RID: 1021
		public float toInfoPow = 1f;

		// Token: 0x040003FE RID: 1022
		public float toInfoMul = 1f;

		// Token: 0x040003FF RID: 1023
		public Shader finalShader;

		// Token: 0x04000400 RID: 1024
		private Material finalMaterial;

		// Token: 0x04000401 RID: 1025
		public CameraSetRenderTexture CameraSetRendTex;

		// Token: 0x04000402 RID: 1026
		public float finalBumpRate = 1f;

		// Token: 0x04000403 RID: 1027
		public float finalDistortionRate = 1f;

		// Token: 0x04000404 RID: 1028
		public Color finalMuddyMaxColor = Color.white;

		// Token: 0x04000405 RID: 1029
		public Color finalMuddyMinColor = Color.white;

		// Token: 0x04000406 RID: 1030
		public float finalMuddyRate = 1f;

		// Token: 0x04000407 RID: 1031
		public float finalAlphaPow = 1f;

		// Token: 0x04000408 RID: 1032
		public Cubemap finalEnv;

		// Token: 0x04000409 RID: 1033
		public Color finalEnvColor = Color.white;

		// Token: 0x0400040A RID: 1034
		[Range(0f, 1f)]
		public float finalEnvRate = 0.2f;

		// Token: 0x0400040B RID: 1035
		private Camera camera;
	}
}
